package yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import yunhe.biz.IProcureBiz;
import yunhe.biz.impl.ProcureBizImpl;
import yunhe.entity.EmpMsg;
import yunhe.entity.Product;
import yunhe.entity.Purchase;
import yunhe.entity.PurchaseAppend;
import yunhe.entity.Storage;
import yunhe.entity.Supplier;
import yunhe.util.GeneralUtil;
@WebServlet(name="PurchaseReturnServlet",urlPatterns="/PurchaseReturnServlet")
public class PurchaseReturnServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		Purchase purchase=new Purchase();
		purchase.setPursId(GeneralUtil.creatSeq("pu"));
		String supplierName=request.getParameter("supplierName");
		IProcureBiz ipb=new ProcureBizImpl();
		Supplier supplier=new Supplier();
		supplier.setSupName(supplierName);
		supplier=ipb.selectSupplierBySupplier(supplier);
		purchase.setSupplier(supplier);
		String operatorName=request.getParameter("operatorName");
		EmpMsg operator=new EmpMsg();
		operator.setName(operatorName);
		operator=ipb.selectEmpMsgByEmpMsgName(operator);
		purchase.setOperator(operator);
		String docMakerName=request.getParameter("docMakerName");
		EmpMsg docMaker=new EmpMsg();
		docMaker.setName(docMakerName);
		docMaker=ipb.selectEmpMsgByEmpMsgName(docMaker);
		purchase.setDocMaker(docMaker);
		String checkerName=request.getParameter("checkerName");
		EmpMsg checker=new EmpMsg();
		checker.setName(checkerName);
		checker=ipb.selectEmpMsgByEmpMsgName(checker);
		purchase.setChecker(checker);
		String keeperName=request.getParameter("keeperName");
		EmpMsg keeper=new EmpMsg();
		keeper.setName(keeperName);
		keeper=ipb.selectEmpMsgByEmpMsgName(keeper);
		purchase.setKeeper(keeper);
		String account1=request.getParameter("accountPaying");
		double accountPaying=Double.parseDouble(account1);
		purchase.setAccountPaying(accountPaying);
		String account2=request.getParameter("accountPaied");
		double accountPaied=Double.parseDouble(account2);
		purchase.setAccountPaied(accountPaied);
		String excu=request.getParameter("excuteState");
		int excuteState=Integer.parseInt(excu);
		purchase.setExcuteState(excuteState);
		boolean flag1=ipb.insertPurchase(purchase);
		PurchaseAppend purchaseAppend=new PurchaseAppend();
		Purchase purchase1=new Purchase();
		purchase1.setPursId(purchase.getPursId());
		purchaseAppend.setPurchase(purchase1);
		String proBarCode=request.getParameter("proBarCode");
		Product product=new Product();
		product.setProBarCode(proBarCode);
		purchaseAppend.setProduct(product);
		String	purQuan=request.getParameter("purQuantity");
		int purQuantity=Integer.parseInt(purQuan);
		purchaseAppend.setPurQuantity(purQuantity);
		String purPri=request.getParameter("purPrice");
		double purPrice=Double.parseDouble(purPri);
		purchaseAppend.setPurPrice(purPrice);
		String purDis=request.getParameter("purDiscount");
		double purDiscount=Double.parseDouble(purDis);
		purchaseAppend.setPurDiscount(purDiscount);
		String purTot=request.getParameter("purTotal");
		double purTotal=Double.parseDouble(purTot);
		purchaseAppend.setPurTotal(purTotal);
		String purStorage=request.getParameter("purStorage");
		Storage storage=new Storage();
		storage.setStoName(purStorage);
		storage=ipb.selectStorageByStorageName(storage);
		purchaseAppend.setStorage(storage);
		boolean flag2=ipb.insertPurchaseAppend(purchaseAppend);
		PrintWriter out=response.getWriter();
		if(flag1==true && flag2==true){
			out.print("<script>alert('退货成功');location.href='/purchase/searchAllHistoryRecord.jsp'</script>");
		}else{
			out.print("<script>alert('退货失败');location.href='/purchase/searchAllHistoryRecord.jsp'</script>");
		}
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		doGet(req, resp);
	}
}
